package com.leetcode.partition4;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author `RKC`
 * @date 2021/11/17 11:51
 */
public class LC318最大单词长度乘积 {

    public static int maxProduct(String[] words) {
        int answer = 0;
        Map<Integer, Integer> maskToWord = new HashMap<>();
        //计算每一个单词的mask
        for (String word : words) {
            int mask = 0;
            for (int i = 0; i < word.length(); i++) mask |= 1 << word.charAt(i) - 'a';
            if (word.length() > maskToWord.getOrDefault(mask, 0)) {
                maskToWord.put(mask, word.length());
            }
        }
        Set<Integer> maskSet = maskToWord.keySet();
        for (Integer mask1 : maskSet) {
            for (Integer mask2 : maskSet) {
                if ((mask1 & mask2) == 0) {
                    answer = Math.max(answer, maskToWord.get(mask1) * maskToWord.get(mask2));
                }
            }
        }
        return answer;
    }
}
